package com.xxwu.springiocdi.config;

import com.xxwu.springiocdi.Component.UserComponent;
import com.xxwu.springiocdi.controller.UserController;
import com.xxwu.springiocdi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
//用来模拟依赖的注入方式
public class DIConfig {
    //1 通过 Autowired 方式注入
    @Autowired
    private UserComponent userComponent;

    //2 通过构造函数注入
    //只有一个构造函数时，Spring知道使用哪个
    //但当有多个构造函数时，Spring不知道用哪个，可以通过 Autowired 指定构造函数
    private UserService userService;
    public DIConfig(UserService userService) {
        this.userService = userService;
    }
    @Autowired  //通过 Autowired 指定构造函数，否则Spring无法判断使用哪个构造函数
    public DIConfig(UserService userService, UserComponent userComponent) {
        this.userService = userService;
        this.userComponent = userComponent;
    }

    //通过set方法注入
    @Autowired
    private UserController userController;
    public void setUserController(UserController userController) {
        this.userController = userController;
    }
    public void print() {
        userComponent.sayHi();
        userService.sayHi();
        userController.sayHi();
    }
}
